package com.ophiux.cs.api.consultBase.impl;

import java.lang.reflect.Type;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.ophiux.cs.api.consultBase.port.IFocusApiPort;
import com.ophiux.cs.application.consultBase.dto.FocusRequest;
import com.ophiux.cs.application.consultBase.dto.FocusResponse;
import com.ophiux.cs.application.consultBase.dto.GetFocusDoctorsRequest;
import com.ophiux.cs.application.consultBase.dto.GetFocusDoctorsResponse;
import com.ophiux.cs.application.consultBase.service.IFocusAppService;
import com.ophiux.cs.application.consultBase.service.IGetFocusDoctorsAppService;
import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.Request;
import com.ophiux.cs.application.core.Result;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class FocusApiService implements IFocusApiPort{
	@Autowired
	IFocusAppService focusAppService;
	@Autowired
	IGetFocusDoctorsAppService getFocusDoctorsAppService;
	
	@Override
	public String Focus(String data) {
		log.info("Focus接口,请求参数："+data);
		Result<FocusResponse> result = null;
		Request<FocusRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<FocusRequest>>() {
					}.getType();
				}
			});
			
			result=focusAppService.Focus(request.getPARAM());
			
			String returnMessage = JsonUtils.toJson(result);
			log.info("Focus接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("Focus接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String GetFocusDoctors(String data) {
		log.info("GetFocusDoctors接口,请求参数："+data);
		Result<List<GetFocusDoctorsResponse>> result = null;
		Request<GetFocusDoctorsRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<GetFocusDoctorsRequest>>() {
					}.getType();
				}
			});
			
			result=getFocusDoctorsAppService.GetFocusDoctors(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("GetFocusDoctors接口,返回参数："+returnMessage);
			return returnMessage;
			
		} catch (Exception e) {
			log.info("GetFocusDoctors接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
	}

}
